package com.dormitory.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dormitory.manage.mapper.CollegeMapper;
import com.dormitory.manage.pojo.College;
import com.dormitory.manage.pojo.Profession;
import com.dormitory.manage.service.CollegeService;
import com.dormitory.manage.service.ProfessionService;
import com.dormitory.manage.vo.CollegeDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description CollegeServiceImpl
 * @date 2023/4/7 0007 23:51
 */
@Service
public class CollegeServiceImpl extends ServiceImpl<CollegeMapper, College> implements CollegeService {
    @Resource
    private ProfessionService professionService;
    /**
    * @description 获取学院名称列表
    *
    * @returnType java.util.List<java.lang.String>
    * @author Administrator
    * @date  23:54
    */
    @Override
    public List<String> getCollegeNameList() {
        List<College> colleges = this.baseMapper.selectList(null);
        List<String> collegeNames = new ArrayList<>();
        for (College college: colleges) {
            collegeNames.add(college.getCollegeName());
        }
        return collegeNames;
    }

    @Override
    public List<CollegeDetailVo> getCollegeListDetail() {
        List<College> collegeList = this.baseMapper.selectList(null);
        ArrayList<CollegeDetailVo> collegeDetails = new ArrayList<>();
        for (College c : collegeList) {
            List<Profession> professions = this.professionService.getAll(c.getCollegeId());
            CollegeDetailVo collegeDetailVo = new CollegeDetailVo();
            collegeDetailVo.setCollegeId(c.getCollegeId());
            collegeDetailVo.setCollegeName(c.getCollegeName());
            collegeDetailVo.setCollegeDean(c.getCollegeDean());
            collegeDetailVo.setCreateTime(c.getCreateTime());
            collegeDetailVo.setProfessions(professions);
            collegeDetails.add(collegeDetailVo);
        }
        return collegeDetails;
    }

    @Override
    public boolean removeCollege(Long id) {
        return this.baseMapper.deleteById(id) > 0;
    }

    @Override
    public College getCollegeInfo(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public boolean updateCollege(College college) {
        College college1 = this.baseMapper.selectById(college.getCollegeId());
        if (null != college1){
           return this.baseMapper.updateById(college) > 0;
        }
        return false;
    }

    @Override
    public boolean addCollege(College college) {
        college.setCreateTime(new Date());
        return this.baseMapper.insert(college) > 0;
    }

    @Override
    public College getCollege(String collegeName) {
        return this.baseMapper.selectOne(new QueryWrapper<College>().eq("college_name", collegeName));
    }
}
